package com.xiyou.luckdraw.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.xiyou.luckdraw.api.converter.ProductCategoryConverter;
import com.xiyou.luckdraw.api.dal.entitys.ProductCategory;
import com.xiyou.luckdraw.api.dal.mapper.luckdraw.ProductCategory1Mapper;
import com.xiyou.luckdraw.api.dal.mapper.luckyelite.ProductCategory2Mapper;
import com.xiyou.luckdraw.api.dto.order.OrderInfoDto;
import com.xiyou.luckdraw.api.dto.product.AddProductCategoryRequest;
import com.xiyou.luckdraw.api.dto.product.AddProductCategoryResponse;
import com.xiyou.luckdraw.api.service.IProductCategoryService;
import com.xiyou.luckdraw.common.constants.ResultCodeConstants;
import com.xiyou.luckdraw.common.exception.ExceptionProcessorUtils;
import com.xiyou.luckdraw.common.utils.ReqUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName: ProductCategoryServiceImpl
 * @Description:
 * @author: limingxing
 * @Date: 2019-10-16 20:16
 */
@Slf4j
@Service
public class ProductCategoryServiceImpl implements IProductCategoryService {

    @Autowired
    private ProductCategory1Mapper productCategory1Mapper;

    @Autowired
    private ProductCategory2Mapper productCategory2Mapper;
    @Autowired
    ProductCategoryConverter productCategoryConverter;
    @Override
    public AddProductCategoryResponse createProductCategory(AddProductCategoryRequest request) {
        log.error("Begin ProductCategoryServiceImpl.addProductCategory request : {}", request);
        AddProductCategoryResponse response = new AddProductCategoryResponse();
        try{
            request.requestCheck();

            String datasourceType = ReqUtils.getDatasourceType();
            Integer row = null;
            ProductCategory productCategory = productCategoryConverter.req2Product(request);
            if(StringUtils.isNotBlank(datasourceType) && "luckdraw".equals(datasourceType)){
                row = productCategory1Mapper.insert(productCategory);
            }else {
                row = productCategory2Mapper.insert(productCategory);
            }
            response.setCode(ResultCodeConstants.SUCCESS.getCode());
            response.setMsg(ResultCodeConstants.SUCCESS.getMessage());
            log.info("ProductCategoryServiceImpl.addProductCategory effect row : {}", row);
        }catch (Exception e){
            log.error("ProductCategoryServiceImpl.addProductCategory Occur Exception : {}", e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }
}
